using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TwoTwoItem : MonoBehaviour
{
    public Slider slider;
    public Text NameText, Numtext, ButText;
    TwoTwoData my_Data;
    public Image image;
    public void init(TwoTwoData twoTwoData)
    {
        my_Data = twoTwoData;
        NameText.text = my_Data.name;
        Numtext.text = my_Data.needNum + "/" + my_Data.aimNum;
        slider.maxValue = my_Data.aimNum;
        slider.value = (float)my_Data.needNum;
        MessMgr.GetSinleton().AddListener("TwoTwoPanel", Refresh);
    }

    private void Refresh()
    {
        if (my_Data.needNum < my_Data.aimNum)
        {
            my_Data.needNum += 1;
            slider.value = (float)my_Data.needNum;
            Numtext.text = my_Data.needNum + "/" + my_Data.aimNum;
            if (my_Data.needNum >= my_Data.aimNum)
            {
                my_Data.state = 1;
                ButText.text = "已完成";
                MessMgr.GetSinleton().RemoveListener("TwoTwoPanel", Refresh);
            }
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        GetComponentInChildren<Button>().onClick.AddListener(But);
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void But()
    {
        Debug.Log(my_Data.state);
        if (my_Data.state == 1)
        {
            image.gameObject.SetActive(true);
            ButText.text = "";
            TwoPanel.Ins.MoneyNum += 100;
            my_Data.state = 2;
        }
    }
}
